package com.example.code06;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class LoginActivity extends AppCompatActivity {
    private Boolean bPwdshow=false;
    private EditText etPwd;
    private EditText etuser;
    private CheckBox etpwd_check;
    private Button btn_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_login);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        final ImageView passshow=findViewById(R.id.pwd_show);
        etPwd=findViewById(R.id.password);
        etuser=findViewById(R.id.user);
        etpwd_check=findViewById(R.id.pwd_remember);
        btn_login=findViewById(R.id.login);

        String spfilename=getResources().getString(R.string.sharepreferences_filename);
        String login_user=getResources().getString(R.string.login_user);
        String login_pwd=getResources().getString(R.string.login_pwd);
        String pwd_check=getResources().getString(R.string.pwd_check);
        SharedPreferences sharedPreferences=getSharedPreferences(spfilename,MODE_PRIVATE);
        String user=sharedPreferences.getString(login_user,null);
        String password=sharedPreferences.getString(login_pwd,null);
        Boolean rememberpwd=sharedPreferences.getBoolean(pwd_check,false);
        if(user!=null| TextUtils.isEmpty(user)){
            etuser.setText(user);
        }
        if(password!=null| TextUtils.isEmpty(password)){
            etPwd.setText(password);
        }
        etpwd_check.setChecked(rememberpwd);

        passshow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bPwdshow=!bPwdshow;
                if(bPwdshow){
                    passshow.setImageResource(R.drawable.baseline_visibility_24);
                    etPwd.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
                }
                else {
                    passshow.setImageResource(R.drawable.baseline_visibility_off_24);
                    etPwd.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                    etPwd.setTypeface(Typeface.DEFAULT);
                }

            }
        });

        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                SharedPreferences.Editor editor=sharedPreferences.edit();
                if(etpwd_check.isChecked()){
                    String password=etPwd.getText().toString();
                    String user=etuser.getText().toString();
                    if (TextUtils.isEmpty(user) || TextUtils.isEmpty(password)) {
                        // 显示错误信息，提示用户输入账号和密码
                        Toast.makeText(LoginActivity.this, "账号和密码不能为空", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    editor.putString(login_user,user);
                    editor.putString(login_pwd,password);
                    editor.putBoolean(pwd_check, true);
                    editor.apply();
                }
                else{
                    editor.remove(login_user);
                    editor.remove(login_pwd);
                    editor.remove(pwd_check);
                    editor.apply();
                }
                Intent intent=new Intent(LoginActivity.this, DetailActivity.class);
                startActivity(intent);
            }
        });

    }


}